package xyz.masaikk;


public class EQuenBackTrack {
    public int n;
    public int answerCount;
    public int [] matrix;
    public EQuenBackTrack(int num){
        this.n=num;
        this.answerCount=0;
        this.matrix=new int[this.n+1];
    }
    private void e(int row){
        if(row == this.n+1){
            this.answerCount++;
            return;
        };
        for(int i = 1; i < this.n+1; i++)//on the way
        {
            matrix[row] = i;
            if( is_legal(row, i) )//if legal, recursive
            {
                matrix[row]=i;
                e(row+1);
            }
        }


    };

    public boolean is_legal(int r, int c){
        for(int i = 1; i < r ; i++)
        {
            if( matrix[i] == c )
                return false;
            if(  ((matrix[i] - c) == (i-r))  ||((matrix[i] - c) == (r-i))  )
                return false;
        }
        return true;
    };

    public void func(){
        this.answerCount = 0;
        e(1);

        return;
    }
    public int getAnswerCount(){
        System.out.println(this.answerCount);
        return this.answerCount;
    };
}


